import React, { createContext, useState, useContext, ReactNode } from "react";

type Language = "zh" | "en";
type Translations = {
  [key: string]: {
    [key: string]: string;
  };
};

interface LanguageContextType {
  language: Language;
  setLanguage: (lang: Language) => void;
  t: (key: string) => string;
}

const LanguageContext = createContext<LanguageContextType | undefined>(
  undefined
);

const translations: Translations = {
  zh: {
    技能列表: "技能列表",
    作业列表: "作业列表",
    同学列表: "同学列表",
    打分记录表: "打分记录表",
    评分查询: "评分查询",
    数据整理: "数据整理",
    导出: "导出",
    导入: "导入",
    添加课程: "添加课程",
    输入新课程名称: "输入新课程名称",
    添加技能: "添加技能",
    输入新技能名称: "输入新技能名称",
    添加作业: "添加作业",
    作业标题: "作业标题",
    添加: "添加",
    输入同学姓名: "输入同学姓名",
    选择同学: "选择同学",
    选择作业: "选择作业",
    选择课程: "选择课程",
    选择技能: "选择技能",
    "选择技能并评分（1-4分）": "选择技能并评分（1-4分）",
    评分: "评分",
    提交评分: "提交评分",
    评分记录: "评分记录",
    同学: "同学",
    作业: "作业",
    课程: "课程",
    技能: "技能",
    平均分: "平均分",
    全部同学: "全部同学",
    全部课程: "全部课程",
    全部技能: "全部技能",
    全部作业: "全部作业",
    暂无数据: "暂无数据",
    语言: "语言",
    中文: "中文",
    英文: "English",
    导出CSV: "导出CSV",
    导入CSV: "导入CSV",
    选择CSV文件: "选择CSV文件",
    "确定要删除这条评分记录吗？": "确定要删除这条评分记录吗？",
    "所选技能已经被评分，请选择其他技能或作业。":
      "所选技能已经被评分，请选择其他技能或作业。",
    修改评分: "修改评分",
    新评分: "新评分",
    选择新评分: "选择新评分",
    确认修改: "确认修改",
    "确定要修改这条评分记录吗？": "确定要修改这条评分记录吗？",
    评语: "评语",
    新评语: "新评语",
    输入新评语: "输入新评语",
  },
  en: {
    技能列表: "Skill List",
    作业列表: "Assignment List",
    同学列表: "Student List",
    打分记录表: "Score Record",
    评分查询: "Score Query",
    数据整理: "Data Organization",
    导出: "Export",
    导入: "Import",
    添加课程: "Add Course",
    输入新课程名称: "Enter new course name",
    添加技能: "Add Skill",
    输入新技能名称: "Enter new skill name",
    添加作业: "Add Assignment",
    作业标题: "Assignment Title",
    添加: "Add",
    输入同学姓名: "Enter student name",
    选择同学: "Select Student",
    选择作业: "Select Assignment",
    选择课程: "Select Course",
    选择技能: "Select Skill",
    "选择技能并评分（1-4分）": "Select skills and score (1-4 points)",
    评分: "Score",
    提交评分: "Submit Score",
    评分记录: "Score Records",
    同学: "Student",
    作业: "Assignment",
    课程: "Course",
    技能: "Skill",
    平均分: "Average Score",
    全部同学: "All Students",
    全部课程: "All Courses",
    全部技能: "All Skills",
    全部作业: "All Assignments",
    暂无数据: "No Data",
    语言: "Language",
    中文: "中文",
    英文: "English",
    导出CSV: "Export CSV",
    导入CSV: "Import CSV",
    选择CSV文件: "Select CSV file",
    "确定要删除这条评分记录吗？":
      "Are you sure you want to delete this score record?",
    "所选技能已经被评分，请选择其他技能或作业。":
      "The selected skill has already been scored. Please choose another skill or assignment.",
    修改评分: "Edit Score",
    新评分: "New Score",
    选择新评分: "Select New Score",
    确认修改: "Confirm Edit",
    "确定要修改这条评分记录吗？":
      "Are you sure you want to modify this score record?",
    评语: "Comment",
    新评语: "New Comment",
    输入新评语: "Enter new comment",
  },
};

export const LanguageProvider: React.FC<{ children: ReactNode }> = ({
  children,
}) => {
  const [language, setLanguage] = useState<Language>("zh");

  const t = (key: string): string => {
    return translations[language][key] || key;
  };

  return (
    <LanguageContext.Provider value={{ language, setLanguage, t }}>
      {children}
    </LanguageContext.Provider>
  );
};

export const useLanguage = () => {
  const context = useContext(LanguageContext);
  if (context === undefined) {
    throw new Error("useLanguage must be used within a LanguageProvider");
  }
  return context;
};
